/*
* create by Gangta
*

var data;

function createCookie(name, value, days) {
if(days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else
var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';') ;
console.log(ca);
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while(c.charAt(0) == ' ')
c = c.substring(1, c.length);
if(c.indexOf(nameEQ) == 0)
return c.substring(nameEQ.length, c.length);
}
return null;
}
*/

//Load Recent data in INDEX PAGE
function recentData(dataContent) {
	dataCall = dataContent + "()";
	var result = eval(dataCall);
}

//Category
function displayAllCategory() {
	$.ajax({
		url : '../admin/allCategory.php',
		success : function(data) {
			$('.main').html(data);
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormAddCategory() {
	$.ajax({
		url : '../admin/addCategory.php',
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = true;
			validateFormAddCategory();
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormEditCategory(id) {
	$.ajax({
		url : '../admin/editCategory.php',
		type : 'POST',
		data : {
			categoryId : id
		},
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = false;
			validateFormAddCategory();
		},
		error : function() {
			alert('error onload');
		}
	})
}

// Category Detail
function displayAllCategoryDetail() {
	$.ajax({
		url : '../admin/allCategoryDetail.php',
		success : function(data) {
			$('.main').html(data);
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormAddCategoryDetail() {
	$.ajax({
		url : '../admin/addCategoryDetail.php',
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = true;
			validateFormAddCategoryDetail();
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormEditCategoryDetail(id) {
	$.ajax({
		url : '../admin/editCategoryDetail.php',
		type : 'POST',
		data : {
			categoryDetailId : id
		},
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = false;
			validateFormAddCategoryDetail();
		},
		error : function() {
			alert('error onload');
		}
	})
}

//Publisher
function displayAllPublisher() {
	$.ajax({
		url : '../admin/allPublisher.php',
		success : function(data) {
			$('.main').html(data);
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormAddPublisher() {
	$.ajax({
		url : '../admin/addPublisher.php',
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = true;
			validateFormAddPublisher();
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormEditPublisher(id) {
	$.ajax({
		url : '../admin/editPublisher.php',
		type : 'POST',
		data : {
			publisherId : id
		},
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = false;
			validateFormAddPublisher();
		},
		error : function() {
			alert('error onload');
		}
	})
}

//Book Stores
function displayAllBookStore() {
	$.ajax({
		url : '../admin/allBookStore.php',
		success : function(data) {
			$('.main').html(data);
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormAddBookStore() {
	$.ajax({
		url : '../admin/addBookStore.php',
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = true;
			validateFormAddBookStore();
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormEditBookStore(id) {
	$.ajax({
		url : '../admin/editBookStore.php',
		type : 'POST',
		data : {
			bookStoreId : id
		},
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = false;
			validateFormAddBookStore();
		},
		error : function() {
			alert('error onload');
		}
	})
}

//Book
function displayAllBook() {
	$.ajax({
		url : '../admin/allBook.php',
		success : function(data) {
			$('.main').html(data);
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormAddBook() {
	$.ajax({
		url : '../admin/addBook.php',
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = true;
			validateFormAddBook();
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormEditBook(id) {
	$.ajax({
		url : '../admin/editBook.php',
		type : 'POST',
		data : {
			bookId : id
		},
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = false;
			validateFormAddBook();
		},
		error : function() {
			alert('error onload');
		}
	})
}

//author
function displayAllAuthor() {
	$.ajax({
		url : '../admin/allAuthor.php',
		success : function(data) {
			$('.main').html(data);
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormAddAuthor() {
	$.ajax({
		url : '../admin/addAuthor.php',
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = true;
			validateFormAddAuthor();
		},
		error : function() {
			alert('error onload');
		}
	})
}

function displayFormEditAuthor(id) {
	$.ajax({
		url : '../admin/editAuthor.php',
		type : 'POST',
		data : {
			authorId : id
		},
		success : function(data) {
			$('.main').html(data);
			document.getElementById("button").disabled = false;
			validateFormAddAuthor();
		},
		error : function() {
			alert('error onload');
		}
	})
}

//validate Email
function validateEmail(email) {
	var atpos = email.indexOf("@");
	var dotpos = email.lastIndexOf(".");
	if(atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
		return false;
	} else
		return true;
}

// Enable Submit Button
function enableSubmitButtonAddCategory() {
	if($('#name').val() != '' && $('#description').val() != '') {
		document.getElementById("button").disabled = false;
	}
}

function enableSubmitButtonAddCategoryDetail() {
	if($('#name').val() != '' && $('#description').val() != '') {
		document.getElementById("button").disabled = false;
	}
}

function enableSubmitButtonAddBook() {
	if($('#name').val() != '' && $('#description').val() != '' && $('#price').val() != '' && $('#page-numb').val() != '' && $('#size').val() != '' && $('#discount').val() != '') {
		document.getElementById("button").disabled = false;
	}
}

function enableSubmitButtonAddPublisher() {
	if($('#name').val() != '' && $('#description').val() != '' && $('#address').val() != '' && $('#home').val() != '') {
		document.getElementById("button").disabled = false;
	}
}

function enableSubmitButtonAddBookStore() {
	if($('#name').val() != '' && $('#description').val() != '' && $('#address').val() != '' && $('#home').val() != '' && $('#phone').val() != '' && validateEmail($('#email').val()) == true) {
		document.getElementById("button").disabled = false;
	}
}

function enableSubmitButtonAddAuthor() {
	if($('#name').val() != '' && $('#description').val() != '' && validateEmail($('#email').val()) == true) {
		document.getElementById("button").disabled = false;
	}
}

//Validate Form
function validateFormAddCategory() {
	$('#name').live('focusout', function() {
		if($('#name').val() == '') {
			$('#check-name').html('Chưa nhập tên tác giả');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddCategory();
	})
	$('#description').live('keypress', function() {
		if($('#description').val() == '') {
			$('#check-description').html('Please enter description');
			document.getElementById("button").disabled = true;
		} else {
			enableSubmitButtonAddCategory();
			$('#check-description').html('');
		}
	})
}

function validateFormAddBook(){
	$('#name').live('focusout', function() {
		if($('#name').val() == '') {
			$('#check-name').html('Chưa nhập tên sách');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddBook();
	}),
	$('#page-numb').live('focusout', function() {
		if($('#name').val() == '') {
			$('#check-page-numb').html('Chưa nhập số trang sách');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddBook();
	}),
	$('#size').live('focusout', function() {
		if($('#size').val() == '') {
			$('#check-size').html('Chưa nhập cỡ sách');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddBook();
	}),
	$('#price').live('focusout', function() {
		if($('#price').val() == '') {
			$('#check-price').html('Chưa nhập giá');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddBook();
	}),
	$('#discount').live('focusout', function() {
		if($('#discount').val() == '') {
			$('#check-discount').html('Chưa nhập chiết khấu');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddBook();
	}),
	$('#description').live('keypress', function() {
		if($('#description').val() == '') {
			$('#check-description').html('Please enter description');
			document.getElementById("button").disabled = true;
		} else {
			enableSubmitButtonAddBook();
			$('#check-description').html('');
		}
	})
}

function validateFormAddCategoryDetail() {
	$('#name').live('focusout', function() {
		if($('#name').val() == '') {
			$('#check-name').html('Chưa nhập tên tác giả');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddCategoryDetail();
	}),
	$('#description').live('keypress', function() {
		if($('#description').val() == '') {
			$('#check-description').html('Please enter description');
			document.getElementById("button").disabled = true;
		} else {
			enableSubmitButtonAddCategoryDetail();
			$('#check-description').html('');
		}
	})
}

function validateFormAddPublisher() {$('#name').live('focusout', function() {
		if($('#name').val() == '') {
			$('#check-name').html('Chưa nhập tên tác giả');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddPublisher();
	}), $('#address').live('focusout', function() {
		if($('#address').val() == '') {
			$('#check-address').html('Chưa nhập địa chỉ');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddPublisher();
	}), $('#home').live('focusout', function() {
		if($('#home').val() == '') {
			$('#check-home').html('Chưa nhập link trang chủ');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddPublisher();
	}), $('#description').live('keypress', function() {
		if($('#description').val() == '') {
			$('#check-description').html('Please enter description');
			document.getElementById("button").disabled = true;
		} else {
			enableSubmitButtonAddPublisher();
			$('#check-description').html('');
		}
	})
}

function validateFormAddBookStore() {$('#name').live('focusout', function() {
		if($('#name').val() == '') {
			$('#check-name').html('Chưa nhập tên tác giả');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddBookStore();
	}), $('#address').live('focusout', function() {
		if($('#address').val() == '') {
			$('#check-address').html('Chưa nhập địa chỉ');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddBookStore();
	}), $('#home').live('focusout', function() {
		if($('#home').val() == '') {
			$('#check-home').html('Chưa nhập link trang chủ');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddBookStore();
	}), $('#description').live('keypress', function() {
		if($('#description').val() == '') {
			$('#check-description').html('Please enter description');
			document.getElementById("button").disabled = true;
		} else {
			enableSubmitButtonAddBookStore();
			$('#check-description').html('');
		}
	}), $('#email').live('focusout', function() {
		if($('#email').val() == '') {
			$('#check-email').html('Chưa nhập email liên lạc');
			document.getElementById("button").disabled = true;
		} else if(validateEmail($('#email').val()) == false) {
			$('#check-email').html('Email không đúng định dạng');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddBookStore();
	}), $('#phone').live('focusout', function() {
		if($('#phone').val() == '') {
			$('#check-phone').html('Chưa nhập số điện thoại');
			document.getElementById("button").disabled = true;
		} else if($('#phone').val().length > 11 || $('#phone').val().length < 8) {
			$('#check-phone').html('Số điện thoại không đúng định dạng');
			document.getElementById("button").disabled = true;
		} else {
			enableSubmitButtonAddBookStore();
		}
	})
}

function validateFormAddAuthor() {$('#name').live('focusout', function() {
		if($('#name').val() == '') {
			$('#check-name').html('Chưa nhập tên tác giả');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddAuthor();
	}), $('#email').live('focusout', function() {
		if($('#email').val() == '') {
			$('#check-email').html('Chưa nhập email liên lạc');
			document.getElementById("button").disabled = true;
		} else if(validateEmail($('#email').val()) == false) {
			$('#check-email').html('Email không đúng định dạng');
			document.getElementById("button").disabled = true;
		} else
			enableSubmitButtonAddAuthor();
	}), $('#description').live('keypress', function() {
		if($('#description').val() == '') {
			$('#check-description').html('Please enter description');
			document.getElementById("button").disabled = true;
		} else {
			enableSubmitButtonAddAuthor();
			$('#check-description').html('');
		}
	})
}


$(document).ready(function() {$('#all-category').click(function() {
		displayAllCategory();
	}), $('#add-category').click(function() {
		displayFormAddCategory();
	}), $('#all-category-detail').click(function() {
		displayAllCategoryDetail();
	}), $('#add-category-detail').click(function() {
		displayFormAddCategoryDetail();
	}), $('#all-book').click(function() {
		displayAllBook();
	}), $('#add-book').click(function() {
		displayFormAddBook();
	}), $('#all-publisher').click(function() {
		displayAllPublisher();
	}), $('#add-publisher').click(function() {
		displayFormAddPublisher();
	}), $('#all-book-store').click(function() {
		displayAllBookStore();
	}), $('#add-book-store').click(function() {
		displayFormAddBookStore();
	}), $('#add-author').click(function() {
		displayFormAddAuthor();
	}), $('#all-author').click(function() {
		displayAllAuthor();
	}),
	recentData($('#load-content').val());
})